package com.myzl.web.controller.coal;

import com.myzl.coal.domain.MessageNotice;
import com.myzl.coal.service.IMessageNoticeService;
import com.myzl.common.annotation.Log;
import com.myzl.common.core.controller.BaseController;
import com.myzl.common.core.domain.AjaxResult;
import com.myzl.common.core.page.TableDataInfo;
import com.myzl.common.enums.BusinessType;
import com.myzl.common.enums.NoticeStatusEnum;
import com.myzl.common.utils.SecurityUtils;
import com.myzl.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户消息通知Controller
 *
 * @author myzl
 * @date 2023-01-10
 */
@RestController
@RequestMapping("/coal/messageNotice")
public class MessageNoticeController extends BaseController {
    @Autowired
    private IMessageNoticeService messageNoticeService;

    /**
     * 查询用户消息通知列表
     */
    @PreAuthorize("@ss.hasPermi('coal:messageNotice:list')")
    @GetMapping("/list")
    public TableDataInfo list(MessageNotice messageNotice) {
        startPage();
        messageNotice.setUserId(SecurityUtils.getUserId());
        List<MessageNotice> list = messageNoticeService.selectMessageNoticeList(messageNotice);
        return getDataTable(list);
    }

    /**
     * 导出用户消息通知列表
     */
    @PreAuthorize("@ss.hasPermi('coal:messageNotice:export')")
    @Log(title = "用户消息通知", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MessageNotice messageNotice) {
        List<MessageNotice> list = messageNoticeService.selectMessageNoticeList(messageNotice);
        ExcelUtil<MessageNotice> util = new ExcelUtil<MessageNotice>(MessageNotice.class);
        util.exportExcel(response, list, "用户消息通知数据");
    }

    /**
     * 获取用户消息通知详细信息
     */
    @PreAuthorize("@ss.hasPermi('coal:messageNotice:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(messageNoticeService.selectMessageNoticeById(id));
    }

    /**
     * 新增用户消息通知
     */
    @PreAuthorize("@ss.hasPermi('coal:messageNotice:add')")
    @Log(title = "用户消息通知", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MessageNotice messageNotice) {
        return toAjax(messageNoticeService.insertMessageNotice(messageNotice));
    }

    /**
     * 修改用户消息通知
     */
    @PreAuthorize("@ss.hasPermi('coal:messageNotice:edit')")
    @Log(title = "用户消息通知", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MessageNotice messageNotice) {
        return toAjax(messageNoticeService.updateMessageNotice(messageNotice));
    }

    /**
     * 清空用户消息通知
     */
    @PreAuthorize("@ss.hasPermi('coal:messageNotice:clean')")
    @Log(title = "用户消息通知", businessType = BusinessType.OTHER)
    @DeleteMapping("/clean")
    public AjaxResult clean() {
        Long userId = SecurityUtils.getUserId();
        return toAjax(messageNoticeService.clean(userId));
    }

    /**
     * 删除用户消息通知
     */
    @PreAuthorize("@ss.hasPermi('coal:messageNotice:remove')")
    @Log(title = "用户消息通知", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(messageNoticeService.deleteMessageNoticeByIds(ids));
    }

    @GetMapping("/getNotice")
    public AjaxResult getNotice() {
        MessageNotice messageNotice = new MessageNotice();
        messageNotice.setUserId(SecurityUtils.getUserId());
        messageNotice.setNoticeStatus(NoticeStatusEnum.NO.getCode());
        List<MessageNotice> list = messageNoticeService.getMessageNoticeList(messageNotice);
        return AjaxResult.success(list);
    }
}
